package com.mumu.jetpackdemo.lifecycle;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleRegistry;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.mumu.jetpackdemo.R;

/**
 * - @Description: Lifecycles使用<br/>
 * - @Author: edie<br/>
 * - @Time: 2019/3/25 下午4:00
 */
public class LifeActivity extends AppCompatActivity {
    private static final String TAG = "LifeActivity";
    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

    public static void startLife(Context context) {
        context.startActivity(new Intent(context, LifeActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life);
        //添加了这一行代码
        getLifecycle().addObserver(new MyObserver());

    }

    @Override
    public Lifecycle getLifecycle() {
//        return super.getLifecycle();
        //重写此方法 使用自己定义的lifeowner 随意分发事件如 onStart()  onStop()
        return mLifecycleRegistry;
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
        mLifecycleRegistry.markState(Lifecycle.State.CREATED);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
        mLifecycleRegistry.markState(Lifecycle.State.DESTROYED);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }
}
